//
//  YCurveView.m
//  DrawCurveDemo
//
//  Created by yan on 16/9/10.
//  Copyright © 2016年 胡岩. All rights reserved.
//

#import "YCurveView.h"

@implementation YCurveView

// drawLayer executes after drawRect , so drawRect needed
- (void)drawRect:(CGRect)rect {
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    UIBezierPath *shapeLayerPath = [UIBezierPath bezierPath];
    UIColor *lineColor = [UIColor blackColor];
    CGFloat shapeStartX = 0;
    CGFloat shapeEndX = 0;
    if (_points.count > 1) {
        NSValue *value1 = [_points firstObject];
        NSValue *value2 = [_points lastObject];
        shapeStartX = [value1 CGPointValue].x;
        shapeEndX = [value2 CGPointValue].x;
    }
    for (int i = 0; i < _points.count - 1; i++) {
        NSValue *value1 = _points[i];
        NSValue *value2 = _points[i + 1];
        if (!strcmp(value1.objCType, @encode(CGPoint)) && !strcmp(value2.objCType, @encode(CGPoint))) {
            CGPoint point1 = [value1 CGPointValue];
            CGPoint point2 = [value2 CGPointValue];
            CGPoint center = [self centerWithP1:point1 p2:point2];
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:point1];
            if (self.type == YCurveViewTypeCurve) {
                [path addQuadCurveToPoint:center controlPoint:[self controlPointHorizontalWithP1:center p2:point1]];
                [path addQuadCurveToPoint:point2 controlPoint:[self controlPointHorizontalWithP1:center p2:point2]];
            } else {
                [path addQuadCurveToPoint:point2 controlPoint:center];
            }
            
            if (_lineColors.count > i) {
                lineColor = _lineColors[i];
            }
            CGContextSetLineWidth(ctx, _lineWidth);
            CGContextAddPath(ctx, path.CGPath);
            CGContextSetStrokeColorWithColor(ctx, lineColor.CGColor);
            CGContextStrokePath(ctx);
            
            if (_bottomMaskColor) {
                [shapeLayerPath appendPath:path];
                CGFloat layerH = layer.frame.size.height;
                [shapeLayerPath addLineToPoint:CGPointMake(shapeEndX - shapeStartX, layerH)];
                [shapeLayerPath addLineToPoint:CGPointMake(shapeStartX, layerH)];
            }
            
        } else {
            NSAssert((strcmp(value1.objCType, @encode(CGPoint)) || strcmp(value2.objCType, @encode(CGPoint))), @"points array have other value");
        }
    }
    if (_bottomMaskColor) {
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.path = shapeLayerPath.CGPath;
        shapeLayer.fillColor = _bottomMaskColor.CGColor;
        [layer addSublayer:shapeLayer];
    }
}

- (CGPoint)centerWithP1:(CGPoint)p1 p2:(CGPoint)p2 {
    return CGPointMake((p1.x + p2.x) / 2.0f, (p1.y + p2.y) / 2.0f);
}

//TODO: draw Vertical lines
- (CGPoint)controlPointVerticalWithP1:(CGPoint)p1 p2:(CGPoint)p2 {
    return CGPointZero;
}

- (CGPoint)controlPointHorizontalWithP1:(CGPoint)p1 p2:(CGPoint)p2 {
    CGPoint point = [self centerWithP1:p1 p2:p2];
    CGFloat differY = fabs(p1.y - point.y);
    if (p1.y > p2.y) {
        point.y -= differY;
    } else {
        point.y += differY;
    }
    return point;
}

#pragma mark - setters and getters
- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    [self setNeedsDisplay];
}

- (CGFloat)lineWidth {
    if (_lineWidth == 0) {
        _lineWidth = 1.0f;
    }
    return _lineWidth;
}

- (void)setLineColors:(NSArray *)lineColors {
    _lineColors = lineColors;
    [self setNeedsDisplay];
}

- (NSArray *)lineColors {
    if (!_lineColors) {
        _lineColors = @[[UIColor blackColor]];
    }
    return _lineColors;
}

- (void)setPoints:(NSArray *)points {
    _points = points;
    [self setNeedsDisplay];
}

- (void)setBottomMaskColor:(UIColor *)bottomMaskColor {
    _bottomMaskColor = bottomMaskColor;
    [self setNeedsDisplay];
}

- (UIColor *)bottomMaskColor {
    if (!_bottomMaskColor) {
        return [UIColor clearColor];
    }
    return _bottomMaskColor;
}

- (void)setType:(YCurveViewType)type {
    _type = type;
    [self setNeedsDisplay];
}


@synthesize
lineWidth       = _lineWidth,
lineColors      = _lineColors,
points          = _points,
bottomMaskColor = _bottomMaskColor,
type            = _type;

@end
